package com.arsn.myf.security.hanlder;

import com.alibaba.fastjson.JSON;
import com.arsn.myf.common.util.result.R;
import com.arsn.myf.common.util.http.ServletUtils;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Serializable;

/**
 * 认证失败处理类 返回未授权
 * @author zhongrj
 */
@Component
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint,Serializable{

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException e){
        int code = HttpServletResponse.SC_UNAUTHORIZED;
        ServletUtils.renderString(response, JSON.toJSONString(R.error(code, e.getMessage())));
    }
}
